package com.cyw.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cyw.common.annotation.Log;
import com.cyw.common.annotation.RepeatSubmit;
import com.cyw.common.core.controller.BaseController;
import com.cyw.common.core.domain.PageQuery;
import com.cyw.common.core.domain.R;
import com.cyw.common.core.page.TableDataInfo;
import com.cyw.common.core.validate.AddGroup;
import com.cyw.common.core.validate.EditGroup;
import com.cyw.common.enums.BusinessType;
import com.cyw.common.utils.poi.ExcelUtil;
import com.cyw.system.domain.TbLabel;
import com.cyw.system.domain.bo.TbLabelBo;
import com.cyw.system.domain.vo.TbLabelVo;
import com.cyw.system.service.ITbLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 标签Controller
 *
 * @author myproject
 * @date 2022-11-22
 */
@Validated
@Api(value = "标签控制器", tags = {"标签管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/label")
public class TbLabelController extends BaseController {

    private final ITbLabelService iTbLabelService;

    /**
     * 查询标签列表
     */
    @ApiOperation("查询标签列表")
    @SaCheckPermission("system:label:list")
    @GetMapping("/list")
    public TableDataInfo<TbLabelVo> list(TbLabelBo bo, PageQuery pageQuery) {
        return iTbLabelService.queryPageList(bo, pageQuery);
    }

    @ApiOperation("查询所有")
    @GetMapping("/getAll")
    public R<List<TbLabel>> getAll() {
        return iTbLabelService.getAll();
    }

    /**
     * 导出标签列表
     */
    @ApiOperation("导出标签列表")
    @SaCheckPermission("system:label:export")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbLabelBo bo, HttpServletResponse response) {
        List<TbLabelVo> list = iTbLabelService.queryList(bo);
        ExcelUtil.exportExcel(list, "标签", TbLabelVo.class, response);
    }

    /**
     * 获取标签详细信息
     */
    @ApiOperation("获取标签详细信息")
    @SaCheckPermission("system:label:query")
    @GetMapping("/{id}")
    public R<TbLabelVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iTbLabelService.queryById(id));
    }

    /**
     * 新增标签
     */
    @ApiOperation("新增标签")
    @SaCheckPermission("system:label:add")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbLabelBo bo) {
        return toAjax(iTbLabelService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改标签
     */
    @ApiOperation("修改标签")
    @SaCheckPermission("system:label:edit")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbLabelBo bo) {
        return toAjax(iTbLabelService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除标签
     */
    @ApiOperation("删除标签")
    @SaCheckPermission("system:label:remove")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iTbLabelService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


}
